package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortCacheMgr;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortRankMgr;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortUnion;
import com.yanqu.road.server.manager.beautyescort.CrossGroupWeekUnionRankListModel;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_GET_UNION_RANK_LIST, desc = "商会榜单")
public class CrossBeautyEscortUnionRankListCmd extends CrossCommand {

    int weekId;
    String unionUid;
    String value;
    boolean getReward;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortUnionWeekRankListReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortUnionWeekRankListReqMsg.parseFrom(packet.getBytes());
        weekId = reqMsg.getWeekId();
        unionUid = reqMsg.getUnionUid();
        value = reqMsg.getValue();
        getReward = reqMsg.getGetReward();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int groupId;
        int thisWeekId = CrossBeautyEscortRankMgr.getThisWeekId();
        if(thisWeekId == weekId){
            groupId = CrossBeautyEscortRankMgr.getGroupId(serverId);
        }else {
            groupId = CrossBeautyEscortRankMgr.getWeekServerGroup(weekId, serverId);
        }
        BeautyEscortProto.BeautyEscortUnionWeekRankListRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortUnionWeekRankListRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setWeekId(weekId);
        respMsg.setMyRank(-1);
        respMsg.setValue("-1");
        respMsg.setGetReward(getReward);
        CrossGroupWeekUnionRankListModel rankListModel = CrossBeautyEscortRankMgr.getCrossGroupWeekUnionRankListModel(weekId, groupId);
        if(rankListModel != null){
            respMsg.addAllRankList(rankListModel.getRankListMsg());
            respMsg.setMyRank(rankListModel.getMyRank(unionUid));
        }
        respMsg.setValue(value);
        CrossBeautyEscortUnion union = CrossBeautyEscortCacheMgr.getCrossBeautyEscortUnion(weekId, unionUid);
        if(union != null){
            respMsg.setValue(String.valueOf(union.getValue()));
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_GET_UNION_RANK_LIST, respMsg));

    }

    @Override
    public void sendData() {

    }
}
